<?php

class PollItem {
	private $ID = null;
	private $PollID;
	private $Label;
	private $Votes;

	private $Controller;

	function __construct($id = null, $Controller){
		$this->Controller = $Controller;

		if($id == null){
			return true;
		}

		$MySQL = $this->Controller->UseApps["mysql"];

		$ItemQuery = $MySQL->query("SELECT * FROM {$MySQL->getPrefix()}poll_items WHERE id = $id");

		if(mysql_num_rows($ItemQuery) <= 0){
			return false;
		}

		$Fetch = mysql_fetch_object($ItemQuery);

		$this->ID = $Fetch->id;
		$this->PollID = $Fetch->poll_id;
		$this->Label = $Fetch->label;
		$this->Votes = $Fetch->votes;
	}

	function update(){
		if(empty($this->Label)){
			return false;
		}

		$MySQL = $this->Controller->UseApps["mysql"];

		if(empty($this->ID)){
			$MySQL->query("INSERT INTO {$MySQL->getPrefix()}poll_items (poll_id, label, votes) VALUES($this->PollID, '$this->Label', 0)");
			$this->ID = mysql_insert_id();
			return true;
		}
		else {
			$MySQL->query("UPDATE {$MySQL->getPrefix()}poll_items SET poll_id = $this->PollID, label = '$this->Label', votes = $this->Votes WHERE id = $this->ID");
			return true;
		}
	}

	function delete(){
		if($this->ID == null){
			return false;
		}

		$MySQL = $this->Controller->UseApps["mysql"];

		$MySQL->query("DELETE FROM {$MySQL->getPrefix()}poll_items WHERE id = $this->ID");
		return true;
	}

	function getID(){
		return $this->ID;
	}

	function getPollID(){
		return $this->PollID;
	}

	function getLabel(){
		return $this->Label;
	}

	function getVotes(){
		return $this->Votes;
	}

	function setLabel($label){
		$this->Label = $label;
	}

	function setVotes($num){
		$this->Votes = $num;
	}

	function setPollID($id){
		$this->PollID = $id;
	}
}

?>